/*
* 任务进度
* */

import 'package:center_beijing/api/statistical_analysis_api.dart';
import 'package:center_beijing/common/logutil.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/widgets/custom_loading_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class TaskProgress extends StatefulWidget {
  @override
  _TaskProgressState createState() => _TaskProgressState();
}

class _TaskProgressState extends State<TaskProgress>
    with SingleTickerProviderStateMixin {
  CustomLoading loading = CustomLoading.withMessage('请稍候...');
  EasyRefreshController _controller = EasyRefreshController();
  List dataSource = [];
  int page = 1;
  int pageSize = 15;
  bool firstLoad = true;

  @override
  void initState() {
    super.initState();
    this.getInfo();
  }

  getInfo() async {
    var pass = {
      'projectId': GlobalConfig.selectFactory.value,
      'page': page,
      'page-size': pageSize,
    };
    var result = await getTaskProgress(pass);
    loading.dismiss();
    if (this.page == 1) {
      dataSource.clear();
    }
    firstLoad = false;
    if (result != null) {
      if (result.length == 0) {
      } else {
        dataSource.addAll(result);
      }
    }
    this.setState(() {});
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Styles.MainBackgroundColor,
      child: this.dataSource.length > 0
          ? EasyRefresh.custom(
              shrinkWrap: true,
              firstRefresh: false,
              controller: _controller,
              header: MaterialHeader(),
              footer: ClassicalFooter(
                  loadingText: '加载中',
                  loadText: '上拉加载',
                  noMoreText: '没有更多数据',
                  loadedText: '加载完成',
                  infoText: '更新于 %T',
                  textColor: Styles.NoMoreColor,
                  showInfo: false),
              emptyWidget: (dataSource.length == 0 && !firstLoad)
                  ? Center(
                      child: Image(
                        image: AssetImage('assets/images/no_data.png'),
                        width: 128,
                        height: 150,
                      ),
                    )
                  : null,
              onRefresh: () async {
                // _controller.finishLoad(success: true, noMore: false);
                if (mounted) {
                  page = 1;
                  this.getInfo();
                }
              },
              onLoad: () async {
                await Future.delayed(Duration(seconds: 2), () {
                  if (mounted) {
                    page++;
                    this.getInfo();
                  }
                });
              },
              slivers: <Widget>[
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (context, index) {
                        return Container(
                          color: Colors.white,
                          margin: EdgeInsets.only(bottom: Styles.spacing8),
                          padding:
                              EdgeInsets.fromLTRB(0, 0, 0, Styles.spacing8),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Container(
                                alignment: Alignment.centerLeft,
                                decoration: BoxDecoration(
                                    border: Border(
                                        bottom: BorderSide(
                                            color: Styles.LineColor,
                                            width: 1))),
                                padding: EdgeInsets.fromLTRB(
                                    Styles.spacing8,
                                    Styles.spacing12,
                                    Styles.spacing8,
                                    Styles.spacing12),
                                child: Text(
                                  '${this.dataSource[index]['taskCode']}',
                                  style: TextStyle(
                                      color: Styles.DetailColor,
                                      fontSize: Styles.NormalFont),
                                ),
                              ),
                              Row(
                                children: [
                                  Container(
                                    padding: EdgeInsets.fromLTRB(
                                        Styles.spacing8,
                                        Styles.spacing8,
                                        Styles.spacing8,
                                        Styles.spacing8),
                                    child: Text(
                                      '完成率',
                                      style: TextStyle(
                                          color: Styles.DetailColor,
                                          fontSize: Styles.MiddleFont17),
                                    ),
                                  ),
                                  Expanded(
                                      child: LinearProgressIndicator(
                                    backgroundColor: Styles.GrayLight,
                                    minHeight: 16.h,
                                    value: this.dataSource[index]['schedule'],
                                    color: Styles.OrangeColor,
                                  )),
                                  Container(
                                    padding: EdgeInsets.fromLTRB(
                                        Styles.spacing8,
                                        Styles.spacing8,
                                        Styles.spacing8,
                                        Styles.spacing8),
                                    child: Text(
                                      '${this.dataSource[index]['schedule'] * 100}%',
                                      style: TextStyle(
                                          color: Styles.LeftTitleColor,
                                          fontSize: Styles.NormalFont),
                                    ),
                                  ),
                                ],
                              ),
                              Container(
                                color: Styles.GrayLight,
                                padding: EdgeInsets.fromLTRB(
                                    Styles.spacing8,
                                    Styles.spacing8,
                                    Styles.spacing8,
                                    Styles.spacing8),
                                margin: EdgeInsets.fromLTRB(
                                    Styles.spacing8,
                                    Styles.spacing4,
                                    Styles.spacing8,
                                    Styles.spacing4),
                                child: Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceBetween,
                                  children: [
                                    Container(
                                      child: Text(
                                        '任务总量',
                                        style: TextStyle(
                                            color: Styles.DetailColor2,
                                            fontSize: Styles.NormalFont),
                                      ),
                                    ),
                                    Container(
                                      child: RichText(
                                          text: TextSpan(children: [
                                            TextSpan(
                                              text:
                                              '${this.dataSource[index]['totalVolume']!=null?this.dataSource[index]['totalVolume']:'0'}',
                                              style: TextStyle(
                                                  fontSize: Styles.MiddleFont17,
                                                  color: Styles.BlueColor),
                                            ),
                                            TextSpan(
                                              text: ' m³',
                                              style: TextStyle(
                                                  fontSize: Styles.NormalFont,
                                                  color: Styles.LeftTitleColor),
                                            )
                                          ])),
                                    ),
                                    Container(
                                      child: RichText(
                                          text: TextSpan(children: [
                                            TextSpan(
                                              text:
                                              '${this.dataSource[index]['totalQuantity']!=null?this.dataSource[index]['totalQuantity']:'0'}',
                                              style: TextStyle(
                                                  fontSize: Styles.MiddleFont17,
                                                  color: Styles.BlueColor),
                                            ),
                                            TextSpan(
                                              text: ' 片',
                                              style: TextStyle(
                                                  fontSize: Styles.NormalFont,
                                                  color: Styles.LeftTitleColor),
                                            )
                                          ])),
                                    ),
                                  ],
                                ),
                              ),
                              Container(
                                color: Styles.GrayLight,
                                padding: EdgeInsets.fromLTRB(
                                    Styles.spacing8,
                                    Styles.spacing8,
                                    Styles.spacing8,
                                    Styles.spacing8),
                                margin: EdgeInsets.fromLTRB(
                                    Styles.spacing8,
                                    Styles.spacing4,
                                    Styles.spacing8,
                                    Styles.spacing4),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Container(
                                      child: Text(
                                        '完成总量',
                                        style: TextStyle(
                                            color: Styles.DetailColor2,
                                            fontSize: Styles.NormalFont),
                                      ),
                                    ),
                                    Container(
                                      child: RichText(
                                          text: TextSpan(children: [
                                        TextSpan(
                                          text:
                                              '${this.dataSource[index]['completeVolume']!=null?this.dataSource[index]['completeVolume']:'0'}',
                                          style: TextStyle(
                                              fontSize: Styles.MiddleFont17,
                                              color: Styles.BlueColor),
                                        ),
                                        TextSpan(
                                          text: ' m³',
                                          style: TextStyle(
                                              fontSize: Styles.NormalFont,
                                              color: Styles.LeftTitleColor),
                                        )
                                      ])),
                                    ),
                                    Container(
                                      child: RichText(
                                          text: TextSpan(children: [
                                        TextSpan(
                                          text:
                                              '${this.dataSource[index]['completeQuantity']!=null?this.dataSource[index]['completeQuantity']:'0'}',
                                          style: TextStyle(
                                              fontSize: Styles.MiddleFont17,
                                              color: Styles.BlueColor),
                                        ),
                                        TextSpan(
                                          text: ' 片',
                                          style: TextStyle(
                                              fontSize: Styles.NormalFont,
                                              color: Styles.LeftTitleColor),
                                        )
                                      ])),
                                    ),
                                  ],
                                ),
                              ),

                              Column(
                                children: [
                                  Container(
                                    color: Styles.GrayLight,
                                    padding: EdgeInsets.fromLTRB(
                                        Styles.spacing8,
                                        Styles.spacing8,
                                        Styles.spacing8,
                                        Styles.spacing8),
                                    margin: EdgeInsets.fromLTRB(
                                        Styles.spacing8,
                                        Styles.spacing4,
                                        Styles.spacing8,
                                        Styles.spacing4),
                                    child: Row(
                                      mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: Text(
                                            '已报废',
                                            style: TextStyle(
                                                color: Styles.DetailColor2,
                                                fontSize: Styles.NormalFont),
                                          ),
                                        ),
                                        Container(
                                          child: RichText(
                                              text: TextSpan(children: [
                                                TextSpan(
                                                  text:
                                                  '${this.dataSource[index]['stateVolume']!=null?this.dataSource[index]['stateVolume']:'0'}',
                                                  style: TextStyle(
                                                      fontSize: Styles.MiddleFont17,
                                                      color: Styles.BlueColor),
                                                ),
                                                TextSpan(
                                                  text: ' m³',
                                                  style: TextStyle(
                                                      fontSize: Styles.NormalFont,
                                                      color: Styles.LeftTitleColor),
                                                )
                                              ])),
                                        ),
                                        Container(
                                          child: RichText(
                                              text: TextSpan(children: [
                                                TextSpan(
                                                  text:
                                                  '${this.dataSource[index]['stateQuantity']!=null?this.dataSource[index]['stateQuantity']:'0'}',
                                                  style: TextStyle(
                                                      fontSize: Styles.MiddleFont17,
                                                      color: Styles.BlueColor),
                                                ),
                                                TextSpan(
                                                  text: ' 片',
                                                  style: TextStyle(
                                                      fontSize: Styles.NormalFont,
                                                      color: Styles.LeftTitleColor),
                                                )
                                              ])),
                                        ),
                                      ],
                                    ),
                                  ),
                                  Container(
                                    color: Styles.GrayLight,
                                    padding: EdgeInsets.fromLTRB(
                                        Styles.spacing8,
                                        Styles.spacing8,
                                        Styles.spacing8,
                                        Styles.spacing8),
                                    margin: EdgeInsets.fromLTRB(
                                        Styles.spacing8,
                                        Styles.spacing4,
                                        Styles.spacing8,
                                        Styles.spacing4),
                                    child: Row(
                                      mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: Text(
                                            '退货量',
                                            style: TextStyle(
                                                color: Styles.DetailColor2,
                                                fontSize: Styles.NormalFont),
                                          ),
                                        ),
                                        Container(
                                          child: RichText(
                                              text: TextSpan(children: [
                                                TextSpan(
                                                  text:
                                                  '${this.dataSource[index]['returnVolume']!=null?this.dataSource[index]['returnVolume']:'0'}',
                                                  style: TextStyle(
                                                      fontSize: Styles.MiddleFont17,
                                                      color: Styles.BlueColor),
                                                ),
                                                TextSpan(
                                                  text: ' m³',
                                                  style: TextStyle(
                                                      fontSize: Styles.NormalFont,
                                                      color: Styles.LeftTitleColor),
                                                )
                                              ])),
                                        ),
                                        Container(
                                          child: RichText(
                                              text: TextSpan(children: [
                                                TextSpan(
                                                  text:
                                                  '${this.dataSource[index]['returnQuantity']!=null?this.dataSource[index]['returnQuantity']:'0'}',
                                                  style: TextStyle(
                                                      fontSize: Styles.MiddleFont17,
                                                      color: Styles.BlueColor),
                                                ),
                                                TextSpan(
                                                  text: ' 片',
                                                  style: TextStyle(
                                                      fontSize: Styles.NormalFont,
                                                      color: Styles.LeftTitleColor),
                                                )
                                              ])),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ],
                          ),
                        );
                      },
                      childCount: dataSource.length,
                    ),
                  ),
                ])
          : firstLoad
              ? Center(
                  child: CircularProgressIndicator(),
                )
              : Center(
                  child: Image(
                    image: AssetImage('assets/images/no_data.png'),
                    width: 128,
                    height: 150,
                  ),
                ),
    );
  }
}
